 subroutine uri_resort_symm(rivv_a, rivo_a, rioo_a, &
                            rivv_b, rivo_b, rioo_b,  &
                            icore, nmo, isymnaux, &
                           isymvrt, isympop, nvmax, nomax, naux8t)
 use mod_ri
 use mod_symm
 use mod_orbit
 use mod_size
 implicit none
 integer, intent(in) :: nmo, nvmax, nomax, naux8t(8)
 integer, intent(in) :: isymnaux(naux, 8)
 integer, intent(in) :: isymvrt(nvmax, 8), isympop(nomax, 8)
 
 real*8, intent(out) :: rivv_a(*), rivo_a(*), rioo_a(*)
 real*8, intent(out) :: rivv_b(*), rivo_b(*), rioo_b(*), icore(*)

 integer irrepn, irrepi, irrepj, irrepa, irrepb 
 integer irrepab, irrepij, irrepai, nfirst, nread, nleft, nsize
 integer numi, numj, numa, numb, numij, numab, numai
 integer i, j, a, b, n, numn, idx0
 integer idxi, idxj, idxa, idxb, idxn, idx1
 integer ioff, idx, idxold, irrep, ioff1
 integer ioffvv(8), ioffvo(8), ioffoo(8)
 integer itmp(naux, 8)
 integer, external :: isymoffso, irpdso
 real*8 :: dtmp, gbsize
 character*7 :: datanm, cirrep
 
  gbsize = 8.d0/(1024*1024*1024)
  ioffvv(1) = 1; ioffvo(1) = 1; ioffoo(1) = 1
  do irrep = 1, nirrep - 1
     numab = irpdso(irrep, vrta, vrta, 1)
     ioffvv(irrep+1) = ioffvv(irrep) + naux8t(irrep)*numab
     numai = irpdso(irrep, vrta, popa, 1)
     ioffvo(irrep+1) = ioffvo(irrep) + naux8t(irrep)*numai
     numij = irpdso(irrep, popa, popa, 1)
     ioffoo(irrep+1) = ioffoo(irrep) + naux8t(irrep)*numij
  enddo
! write(6,*)'naux8t', naux8t
! write(6,*)'popa', popa, vrta
! write(6,*)'popb', popb, vrtb
! write(6,*)'icrsiz', icrsiz
  dtmp = 0.d0
  do irrepab = 1, nirrep
!     write(6,*)'irrepab', irrepab
     irrepn = irrepab 
     do irrepb = 1, nirrep 
        irrepa = dirprd(irrepb, irrepab)
        numa = vrta(irrepa) 
        numb = vrta(irrepb) 
        numn = naux8t(irrepn)
        ioff = ioffvv(irrepab)-1 + &
              (isymoffso(irrepb, irrepab, vrta, vrta, 1)-1)*numn
!        write(6,*)'ioff', ioff 
        if(numa==0.or.numb==0.or.numn==0) cycle
        write(cirrep, '(i1,i1)') irrepa-1,irrepb-1
        datanm = 'evva'//cirrep(1:2) 
!       write(6,*) datanm
        nfirst = 1
        nleft = numn
        nsize = icrsiz/(numa*numb)
!        write(6,*)'nsize', nsize, nleft
        if(nsize==0) then
           write(6,*) 'core size insufficient!'
           write(6,'(1x, a, f8.3, a)') 'It should be at least ',   &
                                       numa*numb*gbsize, ' Gb'
           stop
        endif
        nsize = 10
        do
           nread = min(nsize, nleft)
!          write(6,*)'ntest', nfirst
!          write(6,*)'ntest', nfirst, nread
!          write(6,*)'ntest', nfirst, nread, numa*numb
           call pyscfio_split('cd_a', datanm, icore, nfirst, nread, numa*numb)
           idx1 = 1
!          write(6,*) 'nfirst, nread, numn', nfirst, nread, numn
           do n = nfirst, nfirst+nread-1
              idxn = n + ioff
              call dcopy(numa*numb, icore(idx1), 1, rivv_a(idxn), numn)
              idx1 = idx1 + numa*numb
           enddo
           nleft = nleft - nread
           nfirst = nfirst + nread
           if(nleft==0) exit
        enddo
     enddo
   enddo
   write(6,*) 'rivv_a difference:', dtmp
   !   write(6,*)'finished rivv'     
   dtmp = 0.d0
   do irrepai = 1, nirrep
     irrepn = irrepai
     do irrepi = 1, nirrep
        irrepa = dirprd(irrepi, irrepai)
        numa = vrta(irrepa)
        numi = popa(irrepi)
        numn = naux8t(irrepn)
        ioff = ioffvo(irrepai)-1 + &
            (isymoffso(irrepi, irrepai, vrta, popa, 1)-1)*numn
        if(numa==0.or.numi==0.or.numn==0) cycle
        write(cirrep, '(i1,i1)') irrepa-1,irrepi-1
        datanm = 'evoa'//cirrep(1:2) 
!       write(6,*) 'datanm: ', datanm
        call pyscfio_split('cd_a', datanm, icore, 1, numn, numa*numi)
        idx1 = 1
        do n = 1, numn
           idxn = n + ioff
           call dcopy(numa*numi, icore(idx1), 1, rivo_a(idxn), numn)
           idx1 = idx1 + numa*numi
        enddo
     enddo
   enddo
!   write(6,*)'finished rivo'     
   dtmp = 0.d0
   do irrepij = 1, nirrep
     irrepn = irrepij
     do irrepj = 1, nirrep
        irrepi = dirprd(irrepj, irrepij)
        numi = popa(irrepi)
        numj = popa(irrepj)
        numn = naux8t(irrepn)
        ioff = ioffoo(irrepij)-1 + &
              (isymoffso(irrepj, irrepij, popa, popa, 1)-1)*numn 
        if(numj==0.or.numi==0.or.numn==0) cycle
        write(cirrep, '(i1,i1)') irrepi-1,irrepj-1
        datanm = 'eooa'//cirrep(1:2) 
!       write(6,*) 'datanm: ', datanm
        call pyscfio_split('cd_a', datanm, icore, 1, numn, numj*numi)
        idx1 = 1
        do n = 1, numn
           idxn = n + ioff
           call dcopy(numj*numi, icore(idx1), 1, rioo_a(idxn), numn)
           idx1 = idx1 + numj*numi
        enddo
     enddo
   enddo
   !   write(6,*)'finished rioo'     
!============================================================
!--------------------beta spin-------------------------------
!============================================================
  ioffvv(1) = 1; ioffvo(1) = 1; ioffoo(1) = 1
  do irrep = 1, nirrep - 1
     numab = irpdso(irrep, vrtb, vrtb, 1)
     ioffvv(irrep+1) = ioffvv(irrep) + naux8t(irrep)*numab
     numai = irpdso(irrep, vrtb, popb, 1)
     ioffvo(irrep+1) = ioffvo(irrep) + naux8t(irrep)*numai
     numij = irpdso(irrep, popb, popb, 1)
     ioffoo(irrep+1) = ioffoo(irrep) + naux8t(irrep)*numij
  enddo

  dtmp = 0.d0
  do irrepab = 1, nirrep
     irrepn = irrepab 
     do irrepb = 1, nirrep 
        irrepa = dirprd(irrepb, irrepab)
        numa = vrtb(irrepa) 
        numb = vrtb(irrepb) 
        numn = naux8t(irrepn)
        ioff = ioffvv(irrepab)-1 + &
              (isymoffso(irrepb, irrepab, vrtb, vrtb, 1)-1)*numn 
        if(numa==0.or.numb==0.or.numn==0) cycle
        write(cirrep, '(i1,i1)') irrepa-1,irrepb-1
        datanm = 'evvb'//cirrep(1:2) 
!       write(6,*) datanm
        nfirst = 1
        nleft = numn
        nsize = icrsiz/(numa*numb)
        if(nsize==0) then
           write(6,*) 'core size insufficient!'
           write(6,'(1x, a, f8.3, a)') 'It should be at least ',   &
                                       numa*numb*gbsize, ' Gb'
           stop
        endif
!       nsize = 10
        do 
           nread = min(nsize, nleft)
           call pyscfio_split('cd_b', datanm, icore, nfirst, nread, numa*numb)
           idx1 = 1
!          write(6,*) 'nfirst, nread, numn', nfirst, nread, numn
           do n = nfirst, nfirst+nread-1
              idxn = n + ioff
              call dcopy(numa*numb, icore(idx1), 1, rivv_b(idxn), numn)
              idx1 = idx1 + numa*numb
           enddo
           nleft = nleft - nread
           nfirst = nfirst + nread
           if(nleft==0) exit
        enddo
     enddo
   enddo
   write(6,*) 'rivv_a difference:', dtmp
!   write(6,*)'finished rivv'     
   dtmp = 0.d0
   do irrepai = 1, nirrep
     irrepn = irrepai
     do irrepi = 1, nirrep
        irrepa = dirprd(irrepi, irrepai)
        numa = vrtb(irrepa)
        numi = popb(irrepi)
        numn = naux8t(irrepn)
        ioff = ioffvo(irrepai)-1 + &
            (isymoffso(irrepi, irrepai, vrtb, popb, 1)-1)*numn
        if(numa==0.or.numi==0.or.numn==0) cycle
        write(cirrep, '(i1,i1)') irrepa-1,irrepi-1
        datanm = 'evob'//cirrep(1:2) 
!       write(6,*) 'datanm: ', datanm
        call pyscfio_split('cd_b', datanm, icore, 1, numn, numa*numi)
        idx1 = 1
        do n = 1, numn
           idxn = n + ioff
           call dcopy(numa*numi, icore(idx1), 1, rivo_b(idxn), numn)
           idx1 = idx1 + numa*numi
        enddo
     enddo
   enddo
!   write(6,*)'finished rivo'     
   dtmp = 0.d0
   do irrepij = 1, nirrep
     irrepn = irrepij
     do irrepj = 1, nirrep
        irrepi = dirprd(irrepj, irrepij)
        numi = popb(irrepi)
        numj = popb(irrepj)
        numn = naux8t(irrepn)
        ioff = ioffoo(irrepij)-1 + &
              (isymoffso(irrepj, irrepij, popb, popb, 1)-1)*numn 
        if(numj==0.or.numi==0.or.numn==0) cycle
        write(cirrep, '(i1,i1)') irrepi-1,irrepj-1
        datanm = 'eoob'//cirrep(1:2) 
!       write(6,*) 'datanm: ', datanm
        call pyscfio_split('cd_b', datanm, icore, 1, numn, numj*numi)
        idx1 = 1
        do n = 1, numn
           idxn = n + ioff
           call dcopy(numj*numi, icore(idx1), 1, rioo_b(idxn), numn)
           idx1 = idx1 + numj*numi
        enddo
     enddo
   enddo
 return
 end 
